//
//  STCalendarKitMainCollectionViewCell.m
//  STNewTools
//
//  Created by stoneobs on 2017/7/20.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "STCalendarKitMainCollectionViewCell.h"
static NSString * const STCalendarKitItmeCollectionCellIdentifier = @"STCalendarKitItmeCollectionCellIdentifier";
@interface STCalendarKitMainCollectionViewCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView            *collectionView;


@end
@implementation STCalendarKitMainCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configSubView];
    }
    return self;
}
#pragma mark --Geter And Seter
- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
        [flow setScrollDirection:UICollectionViewScrollDirectionVertical];
        flow.minimumLineSpacing = 0;
        flow.minimumInteritemSpacing = 0;
        flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flow];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[STCalendarKitItemCollectionViewCell class]
            forCellWithReuseIdentifier:STCalendarKitItmeCollectionCellIdentifier];
        
    }
    return _collectionView;
    
}
- (void)setMonthModel:(STCalendarKitMonthModel *)monthModel{

    _monthModel = monthModel;
    [self.collectionView reloadData];

}
#pragma mark --SubView
- (void)configSubView{
    
    [self addSubview:self.collectionView];
    
}
#pragma --mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.monthModel.allDaysArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWith, itemWith);
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    STCalendarKitItemCollectionViewCell * item = [collectionView dequeueReusableCellWithReuseIdentifier:STCalendarKitItmeCollectionCellIdentifier forIndexPath:indexPath];
    item.model = self.monthModel.allDaysArray[indexPath.row];
    [item setButtonAction:^(UIButton * button){
        [weakSelf buttonAction:button itemIndexPath:indexPath];
    }];
    return item;
}
#pragma --mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)buttonAction:(UIButton*)button itemIndexPath:(NSIndexPath*)itemIndexPath{

  //  [self.collectionView reloadItemsAtIndexPaths:@[itemIndexPath]];
    if (self.buttonAction) {
        self.buttonAction(button, itemIndexPath);
    }
}
- (void)reloadData{
    [self.collectionView reloadData];
}
@end
